# coding=UTF-8
import datetime
import time
import chinese_calendar


def get_date_time_str():
    return datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')


def get_date_str():
    return datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')


def get_format_date_str(format):
    return datetime.datetime.strftime(datetime.datetime.now(), format)


def get_time_str():
    return datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S')


def date_to_str(date, format):
    return datetime.datetime.strftime(date, format)


def get_date_time():
    return datetime.datetime.now()


def add_day(date, num):
    return date - datetime.timedelta(days=-num)


def add_hour(date, num):
    return date - datetime.timedelta(hours=-num)


def add_minutes(date, num):
    return date - datetime.timedelta(minutes=-num)


# 字符串转日期
def str_to_date(date_str, format):
    return datetime.datetime.strptime(date_str, format)


def date_str_to_number_tuple(date_str):
    date_list = date_str.split("-")
    year = date_list[0]
    month = date_list[1]
    day = date_list[2]
    return [int(year), int(month), int(day)]


# 计算两个日期相差多少天days
def diff_days(beg_date, end_date):
    start_date_str = parse_date_str(beg_date)
    end_date_str = parse_date_str(end_date)
    start_year_month_day = date_str_to_number_tuple(start_date_str)
    end_year_month_day = date_str_to_number_tuple(end_date_str)
    return (datetime.date(*end_year_month_day) - datetime.date(*start_year_month_day)).days


# 计算两个日期串相差多少天days
# 举例 diff_days_str('2020-12-02', '2020-12-01') = -1
def diff_days_str(beg_date_str, end_date_str):
    start_year_month_day = date_str_to_number_tuple(beg_date_str)
    end_year_month_day = date_str_to_number_tuple(end_date_str)
    return (datetime.date(*end_year_month_day) - datetime.date(*start_year_month_day)).days


# 计算两个日期串相差多少天days
# 举例 diff_month_str('2020-11-02', '2020-12-01') = 1
def diff_months_str(beg_date_str, end_date_str):
    year0 = int(beg_date_str[0:4])
    month0 = int(beg_date_str[5:7])
    year1 = int(end_date_str[0:4])
    month1 = int(end_date_str[5:7])
    return (year1 - year0) * 12 + (month1 - month0)


def parse_date_time_str(date):
    return datetime.datetime.strftime(date, '%Y-%m-%d %H:%M:%S')


def parse_date_str(date):
    return datetime.datetime.strftime(date, '%Y-%m-%d')


def parse_date_str_f(date, format):
    return datetime.datetime.strftime(date, format)


# 获取时间戳 "%Y-%m-%d %H:%M:%S"
def time_to_timestamp(date_str):
    timeArray = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(timeArray))


# 获取时间戳 "%Y-%m-%d"
def date_to_timestamp(date_str):
    timeArray = time.strptime(date_str, "%Y-%m-%d")
    return int(time.mktime(timeArray))


# 获取时间戳 "%Y-%m-%d %H:%M:%S"
def datetime2timestamp(datetime_str):
    return int(time.mktime(time.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")))


# 时间戳转 datetime
def str_to_timeArray(date_str):
    return time.strptime(date_str, "%Y-%m-%d %H:%M:%S")


# 时间戳转日期 "%Y-%m-%d %H:%M:%S"
def timestamp_to_time_str(timeStamp):
    timeArray = time.localtime(timeStamp)
    return time.strftime("%Y-%m-%d %H:%M:%S", timeArray)


# 时间戳转日期 "%Y-%m-%d"
def timestamp_to_date_str(timeStamp):
    timeArray = time.localtime(timeStamp)
    return time.strftime("%Y-%m-%d", timeArray)


# 时间戳转 datetime
def timestamp_to_timeArray(timeStamp):
    return time.localtime(timeStamp)


# 获取时间戳
def get_timestamp_now():
    return int(round(time.time()))


# 获取时间戳
def time_stamp_s():
    return int(round(time.time()))


# compare 时间比较(%Y-%m-%d) 返回比较大的时间序号 0相等
def compare_date_str(date1, date2):
    # 获取时间戳 "%Y-%m-%d"
    time1 = int(time.mktime(time.strptime(date1, "%Y-%m-%d")))
    time2 = int(time.mktime(time.strptime(date2, "%Y-%m-%d")))
    if time1 > time2:
        return 1
    elif time1 < time2:
        return 2
    else:
        return 0


def add_day_str(date_str, num):
    date = str_to_date(date_str, '%Y-%m-%d')
    date = add_day(date, num)
    return date_to_str(date, '%Y-%m-%d')


# 判断是周几
def isoweekday(date):
    return date.isoweekday()


# 判断是周几
def isoweekdaystr(date_str):
    return str_to_date(date_str, '%Y-%m-%d').isoweekday()


# true 有效 false 无效
def valid_day(date_str):
    a = chinese_calendar.utils.is_holiday(str_to_date(date_str, '%Y-%m-%d'))
    weeks_day = isoweekdaystr(date_str)
    b = (weeks_day == 6 or weeks_day == 7)
    return (not a and not b)


# 找到最近有效期
# flag 1:向后找 -1:向前找
def find_valid_day(date_str, flag):
    while not valid_day(date_str):
        date_str = add_day_str(date_str, flag)
    return date_str


# 找到最近有效期
def find_last_valid_day():
    return find_valid_day(get_date_str(), -1)


if __name__ == '__main__':
    print(timestamp_to_time_str(1607563200))
    # days = diff_days_str('2020-12-02', '2020-12-01')
    # days = diff_months_str('2020-11-02', '2021-02-11')
    # days = add_day_str('2020-12-02', 1)
    date_str = find_valid_day('2021-03-06', -1)
    print(date_str)
